package com.kude.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kude.entity.Product;


/**
 * A data access object (DAO) providing persistence and search support for
 * Product entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see common.dao.Product
 * @author MyEclipse Persistence Tools
 */

public class ProductDAO extends BaseDAO<Product,Integer>{
	private static final Logger log = LoggerFactory.getLogger(ProductDAO.class);
	// property constants
	public static final String IMAGE_PATH = Product.IMAGE_PATH;
	public static final String TYPE_NAME = Product.TYPE_NAME;
	public static final String INTRODUCTION = Product.INTRODUCTION;
	public static final String PRODUCT_CATEGORY = Product.PRODUCT_CATEGORY;
	public static final String IS_ONSALE = Product.IS_ONSALE;
	public static final String IS_NEW = Product.IS_NEW;
	
	public ProductDAO() {
		super(Product.class);
		// TODO Auto-generated constructor stub
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Product instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Product as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByImagePath(Object imagePath) {
		return findByProperty(IMAGE_PATH, imagePath);
	}

	public List findByTypeName(Object typeName) {
		return findByProperty(TYPE_NAME, typeName);
	}


	public List findByIntroction(Object introduction) {
		return findByProperty(INTRODUCTION, introduction);
	}

	public List findByIsOnsale(Object isOnsale){
		return findByProperty(IS_ONSALE, isOnsale);
	}
	
	public List findByIsNew(Object isNew){
		return findByProperty(IS_NEW, isNew);
	}


	public List findAll() {
		log.debug("finding all Product instances");
		try {
			String queryString = "from Product";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Product merge(Product detachedInstance) {
		log.debug("merging Product instance");
		try {
			Product result = (Product) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Product instance) {
		log.debug("attaching dirty Product instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Product instance) {
		log.debug("attaching clean Product instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}